<template>
  <el-dropdown trigger="click" @command="handleCommand">
    <span class="el-dropdown-link">
      {{ checkModuleMenuName }}<i class="el-icon-arrow-down el-icon--right"/>
    </span>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item v-for="item in routerMenu" :key="item.id" :command="item">
        {{ item.name }}
    </el-dropdown-item></el-dropdown-menu>
  </el-dropdown>
</template>

<script>
// import { getRouterList } from '@/api/system/menu'
// import { filterAsyncRouter } from '@/store/modules/permission'
// import { constantRouterMap } from '@/router/routers'
// import Router from 'vue-router'
import Cookies from 'js-cookie'
export default {
  name: 'HeaderNavigation',
  components: {},
  mixins: [],
  props: {},
  data() {
    return {
      productName: ''
    }
  },
  computed: {
    routerMenu: {
      get() {
        return this.$store.state.routerMenu.menuList
      }
    },
    checkModuleMenuName: {
      get() {
        return this.$store.state.routerMenu.checkModuleMenuName
      }
    }
  },
  created() {},
  mounted() {
    this.productName = sessionStorage.getItem('productName') || this.$t('changeProduct')
  },
  methods: {
    handleCommand(command) {
      // 都是异步的 重置
      if (command.link && command.url) {
        window.open(command.url)
      } else {
        this.$store.dispatch('ResetRoutes')
        this.$store.dispatch('tagsView/delAllViews').then(() => {
          Cookies.set('routerMenu', command.id)
          Cookies.set('routeMenuHomePage', command.homePage)
          // 防止当前页面不刷新
          this.$router.replace({ path: '/', query: { t: Date.now() }})
          // ?????
          // setTimeout(() => {
          //   this.$router.push({ name: Cookies.get('routeMenuHomePage') })
          // }, 500)
        })
        this.$store.dispatch('setModuleMenuName', command.name)
        sessionStorage.setItem('productName', command.name)
      }
      this.productName = command.name
      sessionStorage.setItem('productName', command.name)
      // 勿删!!!!
      // // addRoutes不会置空 新建一个全新的 Router，然后将新的 Router.matcher 赋给当前页面的管理 Router，以达到更新路由配置的目的
      // this.$router.matcher = new Router({
      //   mode: 'hash',
      //   // mode: 'history',
      //   scrollBehavior: () => ({ y: 0 }),
      //   routes: constantRouterMap
      // })
      // getRouterList({ id: command }).then(res => {
      //   const routes = res.data
      //   const asyncRouter = filterAsyncRouter(routes)
      //   asyncRouter.push({ path: '*', redirect: '/dashboard', hidden: true })
      //   this.$store.dispatch('GenerateRoutes', asyncRouter).then(() => {
      //     // 添加路由
      //     this.$router.addRoutes(asyncRouter)
      //   })
      //   Cookies.set('routerMenu', command)
      // })
    }
  }
}
</script>

<style scoped lang="scss">
.el-dropdown {
    vertical-align: text-bottom;
    .el-dropdown-link{
        cursor: pointer;
    }
}
</style>
